package com.jf.cloud.service.sysmanager.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jf.cloud.component.redis.global.services.GlobalRedisService;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.constant.SysConstant;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Date: 2017/12/5
 * Time: 18:04
 * Description:token 处理器
 */
@Component
public class TokenHandler {

    Logger log = LoggerFactory.getLogger(TokenHandler.class);
    
    @Autowired
    private GlobalRedisService redisService;

    /**
     * 存放后台 token
     *
     * @param token
     * @param uid
     */
    public void putSysToken(String token, String uid, String content) {
        String oldToken = redisService.getString(SysConstant.UID_SYSTEM_TOKEN_PREFIX + uid);
        if (oldToken != null && !oldToken.equals("")) {
            redisService.removeString(SysConstant.TOKEN_SYSTEM_PREFIX + oldToken);
        }
        redisService.putString(SysConstant.UID_SYSTEM_TOKEN_PREFIX + uid, token);
        redisService.putString(SysConstant.TOKEN_SYSTEM_PREFIX + token, content);
    }

    /**
     * 通过用户token获取用户ID
     *
     * @param token
     * @return
     */
    public String getSysStaffByToken(String token) {
        return redisService.getString(SysConstant.TOKEN_SYSTEM_PREFIX + token);
    }

    /**
     * 移除后台token、uid
     * 
     * @author 朱建文 创建时间：2017年12月20日 下午1:55:12
     */
    public void removeSysToken(String token, String uid){
    	redisService.removeString(SysConstant.UID_SYSTEM_TOKEN_PREFIX + uid);
    	redisService.removeString(SysConstant.TOKEN_SYSTEM_PREFIX + token);
    }

    /**
     * 存放前台用户 token
     *
     * @param token
     * @param uid
     */
    public void putToken(String token, String userinfo,String uid) {
        String oldToken = redisService.getString(SysConstant.UID_2_TOKEN_PREFIX + uid);
        if (oldToken != null && !oldToken.equals("")) {
            redisService.removeString(SysConstant.TOKEN_PREFIX + oldToken);
        }
        redisService.putString(SysConstant.UID_2_TOKEN_PREFIX + uid, token);
        redisService.putString(SysConstant.TOKEN_PREFIX + token, userinfo);
    }

    /**
     * 通过用户token获取用户ID(不区分前后台)
     *
     * @param token
     * @return
     */
    public String getCommUserInfoByToken(String token) {
        String uid = redisService.getString(SysConstant.TOKEN_SYSTEM_PREFIX + token);
        if (StringUtil.isNotEmpty(uid)){
            if (uid.equals("null")){
                uid = redisService.getString(SysConstant.TOKEN_PREFIX + token);
                if (StringUtil.isNotEmpty(uid)){
                    if (uid.equals("null")){
                        return null;
                    }
                }
            }
        } else {
            uid = redisService.getString(SysConstant.TOKEN_PREFIX + token);
            if (StringUtil.isNotEmpty(uid)){
                if (uid.equals("null")){
                    return null;
                }
            }
        }
        return uid;
    }

    /**
     * 移除后台token、uid
     * 
     * @author 朱建文 创建时间：2017年12月20日 下午1:55:12
     */
    public void removeToken(String token, String uid){
    	redisService.removeString(SysConstant.UID_2_TOKEN_PREFIX + uid);
    	redisService.removeString(SysConstant.TOKEN_PREFIX + token);
    }
}
